博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
yii图片上传
阅读量:6868 次
发布时间:2019-06-26

本文共 3530 字,大约阅读时间需要 11 分钟。

首先感谢这里的博主,提供了思路,不过在调用 $model->b_image->extensionName 这个时,总提示错误,调呀调的,调烦了,就自己写一个吧。

其他不多说,看步骤

一、下载yii官方提供的图片处理扩展  具体配置跟官方的一样,这个扩展提供了两套调用方式,我选择了第一种,就是在配置文件里进行添加组件的方式,来张图,有图有真象哈

二、在protected目录下新建一utils目录,在目录下新建Upload.php文件,代码如下

request->baseUrl.'images/uploads/'.$name; //图片处理 $image = Yii::app()->image->load($tmp_name); $image->resize($width, $height)->rotate(0)->quality(100)->sharpen(20); $image->save($path); // move_uploaded_file($tmp_name,$path); return $path; } }}

三、在 protected/config/main.php 这个文件里,把刚才的utils目录载入

'import'=>array(

'application.models.*',
'application.components.*',
'application.extensions.*',
'application.helpers.*',
'application.utils.*',
'application.modules.boss.modules.srbac.controllers.SBaseController',
),

四、写代码

model部分

添加图片验证规则,其他规则根据自己需要进行添加,这里我只添加需要展示的代码(views和controller一样,不再说明了)

public function rules()	{		return array(			array('b_image', 'file', 'types' => 'jpg,gif,png', 'on' => 'insert'),		);	}

views

beginWidget('CActiveForm', array('id'=>'iot-books-form',// Please note: When you enable ajax validation, make sure the corresponding// controller action is handling ajax validation correctly.// There is a call to performAjaxValidation() commented in generated controller code.// See class documentation of CActiveForm for details on this.'enableAjaxValidation'=>false,'htmlOptions'=>array('enctype'=>'multipart/form-data'),//这个一定要加,否则获取不到图片)); ?>
labelEx($model,'b_image'); ?>
isNewRecord){ ?>
60,'maxlength'=>255)); ?>
error($model,'b_image'); ?>

controller

/**	 * Creates a new model.	 * If creation is successful, the browser will be redirected to the 'view' page.	 */	public function actionCreate()	{		$model=new IotBooks;		// Uncomment the following line if AJAX validation is needed		// $this->performAjaxValidation($model);		if(isset($_POST['IotBooks']))		{			$imgUrl = '';			if($_FILES){				$name = $_FILES['IotBooks']['name']['b_image']; //上传图片原名				$type = $_FILES['IotBooks']['type']['b_image']; //上传图片mime类型				$tmp_name = $_FILES['IotBooks']['tmp_name']['b_image']; //上传图片临时存放位置				$width = 100; //缩略图宽				$height = 100; //缩略图高				$imgUrl = Upload::createImageLink($name, $type, $tmp_name, $width, $height);			}			$model->attributes = $_POST['IotBooks'];			$model->b_image = $imgUrl;			if($model->save())				$this->redirect(array('view','id'=>$model->bid));		}		$this->render('create',array(			'model'=>$model,		));	}	/**	 * Updates a particular model.	 * If update is successful, the browser will be redirected to the 'view' page.	 * @param integer $id the ID of the model to be updated	 */	public function actionUpdate($id)	{		$model=$this->loadModel($id);		// Uncomment the following line if AJAX validation is needed		// $this->performAjaxValidation($model);		if(isset($_POST['IotBooks']))		{				$imgUrl = $model->b_image;			if($_FILES && ($_FILES['IotBooks']['name']['b_image'] != '')){				$name = $_FILES['IotBooks']['name']['b_image']; //上传图片原名				$type = $_FILES['IotBooks']['type']['b_image']; //上传图片mime类型				$tmp_name = $_FILES['IotBooks']['tmp_name']['b_image']; //上传图片临时存放位置				$width = 100; //缩略图宽				$height = 100; //缩略图高				$imgUrl = Upload::createImageLink($name, $type, $tmp_name, $width,$height);			}			$model->attributes = $_POST['IotBooks'];			$model->b_image = $imgUrl;			if($model->save())				$this->redirect(array('view','id'=>$model->bid));		}		$this->render('update',array(			'model'=>$model,		));	}

完工了,试一下吧

 

 

 

转载于:https://www.cnblogs.com/debmzhang/p/3403483.html

你可能感兴趣的文章
Android开发工具--adb的使用
查看>>
输入框的阴影去除,两种方法!
查看>>
threading模块
查看>>
tcp socket的发送与接收缓冲区(转)
查看>>
session简单存储的购物车系统
查看>>
【noi 2.6_8786】方格取数(DP)
查看>>
SCSS笔记
查看>>
51nod 1459 迷宫游戏【最短路拓展】
查看>>
chrome 74 版本的chromedriver下载地址
查看>>
HDU 3068 最长回文( Manacher模板题 )
查看>>
Codeforces 787A The Monster( 拓展欧几里德 )
查看>>
杭电2035人见人爱A^B
查看>>
一个优秀的Unity3d开发者必备的几种设计模式
查看>>
mysql日期加一个天数获得新的日期
查看>>
彩票生成器
查看>>
浅谈Android反调试 之 PTRACE_TRACEME
查看>>
C++程设实验项目三:黑白棋与基于UCT算法的AI
查看>>
django博客项目-设置django为中文语言
查看>>
[转] Haproxy、Keepalived双主高可用负载均衡
查看>>
openGL如何在改变窗口大小时,使自己的图形不被拉伸
查看>>